/**
 * creates a new instance of an item of a given ItemClass (ex a Item template for the Item Editor)
 * @author zulianp@lu.unisi.ch & remo.lemma@lu.unisi.ch
 */
package ch.unisi.inf.projects.java.uom.gui.commands;

import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.KeyStroke;
import javax.swing.ImageIcon;

import ch.unisi.inf.projects.java.uom.elements.*;
import ch.unisi.inf.projects.java.uom.gui.UOMImages;

public final class ItemCreateCommand extends Command {
	
	/** We are not going to serialize this class atm */
	private static final long serialVersionUID = -1L;
	
	private ItemClass itemClass;
	
	public ItemCreateCommand (final ItemClass itemClass, final boolean icon)
	{
		super();
		if (!icon)
			putValue(NAME,"Create new item");
		else
			putValue(SMALL_ICON, new ImageIcon(getClass().getResource(UOMImages.NEW_ITEM.getFileName())));
        if (itemClass != null)
        	putValue(SHORT_DESCRIPTION,"Create a new item of category " + itemClass.getName());
		putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_N , Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
        this.itemClass = itemClass;
	}
	
    public void actionPerformed(final ActionEvent ev)
    {  
    	ItemManager.instance().itemAdd(itemClass, false);
    	fireActionPerformed();
    }
    
    public void setItemClass (final ItemClass itemClass)
    {
    	this.itemClass = itemClass;
    	if (this.itemClass != null)
        	putValue(SHORT_DESCRIPTION,"Create a new item of category " + itemClass.getName());
    	else
    		putValue(SHORT_DESCRIPTION, null);
    }
}